package com.wgts.wgts.mvp.view.activity.message;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMessageDetailsActivityComponent;
import com.wgts.wgts.dagger.module.MessageActivityModule;
import com.wgts.wgts.mvp.presenter.IMessageActivityPresenter;
import com.wgts.wgts.mvp.view.IMessageActivityView;
import com.wgts.wgts.netmodle.resp.MessageListEntity;
import com.wgts.wgts.netmodle.resp.UnreadMessageEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/10/2216:33
 *
 */public class MessageDetailsActivity extends BaseActivity implements IMessageActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.message_details_recycle)
    RecyclerView messageDetailsRecycle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    public static String CODE_TYPE = "codeType";
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private MessageDetailsAdapter adapter;

    private List<MessageListEntity> messageListEntities;

    @Inject
    IMessageActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_details_activity);
        ButterKnife.bind(this);
        includeTitle.setText("消息中心");
        initView();
    }

    @Override
    public void init() {
        DaggerMessageDetailsActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .messageActivityModule(new MessageActivityModule(this))
                .build()
                .inject(this);
        messageListEntities = new ArrayList<>();
    }

    private void initView() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);
        messageDetailsRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MessageDetailsAdapter(this, messageListEntities);
        messageDetailsRecycle.setAdapter(adapter);
        load();
        presenter.initList();
    }

    @Override
    public void unreadMessage(List<UnreadMessageEntity> entity) {
    }

    @Override
    public void cleadMessage(String typeCodes) {
    }

    @Override
    public void initMessageList(List<MessageListEntity> entities, boolean noMore) {
        messageListEntities.clear();
        adapter.setNoMore(noMore);
        messageListEntities.addAll(entities);
        adapter.notifyDataSetChanged();
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        if (entities.size() > 0){
            noneDataLy.setVisibility(View.GONE);
            netErrorLy.setVisibility(View.GONE);
        }else {
            noneDataLy.setVisibility(View.VISIBLE);
            netErrorLy.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadMore(List<MessageListEntity> entities, boolean noMore) {
        refreshLayout.finishLoadmore();
        messageListEntities.addAll(entities);
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
    }

    private void load() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore();
            }
        });
    }

    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
        noneDataLy.setVisibility(View.GONE);
        netErrorLy.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.back_btn_ly)
    public void onViewClicked() {
        finish();
    }
}
